<template>
  <div class="basetable" element-loading-text="拼命加载中">
    <!-- v-loading 设置加载 -->
    <div class="selectMenu">
      <el-button type="primary" size="medium" @click="add">新增</el-button>

      <el-dialog
        title="添加用户"
        :visible.sync="dialogFormVisiblea"
        width="600px"
      >
        <!-- 新增 -->
        <el-form :model="form">
          <el-form-item label="用户名" :label-width="formLabelWidth">
            <el-input
              v-model="form.username"
              auto-complete="off"
              @change="changea()"
              class="inputa"
            ></el-input>
          </el-form-item>

          <el-form-item label="姓名" :label-width="formLabelWidth">
            <el-input
              v-model="form.nickname"
              auto-complete="off"
              @change="changea()"
              class="inputa"
            ></el-input>
          </el-form-item>

          <el-form-item label="ID" :label-width="formLabelWidth">
            <el-input
              v-model="form.id"
              :disabled="true"
              auto-complete="off"
              @change="changea()"
              class="inputa"
            ></el-input>
          </el-form-item>

          <el-form-item label="密码" :label-width="formLabelWidth">
            <el-input
              v-model="form.password"
              auto-complete="off"
              @change="changea()"
              class="inputa"
            ></el-input>
          </el-form-item>
          <el-form-item label="头像" :label-width="formLabelWidth">
            <el-input
              v-model="input"
              auto-complete="off"
              @change="changea()"
              class="inputa"
            ></el-input>
          </el-form-item>
          <el-form-item label="状态" :label-width="formLabelWidth">
            <label class="labela"
              ><input
                v-model="radio"
                type="radio"
                value="0"
                @change="getRadioVal"
              />启用</label
            >
            <label
              ><input
                v-model="radio"
                type="radio"
                value="1"
                @change="getRadioVal"
              />禁用</label
            >
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <!-- 设置触发更新的方法 -->
          <el-button type="primary" :plain="true" @click="updatea"
            >确 定</el-button
          >
        </div>
      </el-dialog>
    </div>
    <div class="tableMain">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" width="100" label="序号"></el-table-column>
        <el-table-column prop="username" label="用户名"></el-table-column>
        <el-table-column prop="nickname" label="姓名"></el-table-column>

        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <!-- <span v-if="scope.row.status == 0">禁用</span>
            <span v-else>启用</span> -->
            <span>{{ scope.row.status === 1 ? "启用" : "禁用" }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 点击编辑进入编辑页面进行编辑表格数据 -->

            <el-button
              size="medium"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="medium"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 下面这个用来设置点击添加按钮的弹出框，里面可以进行嵌套表格来展示弹出的表格信息,使用下面的:visible.sync来控制显示与否 -->
    <!-- 里面绑定的是我们新设置的值，填写完成后，将我们这个新值塞到页面中所有的数据当中去 -->
    <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="600px">
      <!-- 在el-dialog中进行嵌套el-form实现弹出表格的效果 -->
      <el-form :model="form">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input
            v-model="form.username"
            auto-complete="on"
            @change="changea()"
            style="width: 50%"
          ></el-input>
        </el-form-item>

        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input
            v-model="form.nickname"
            auto-complete="on"
            @change="changea()"
            style="width: 50%"
          ></el-input>
        </el-form-item>

        <el-form-item label="头像" :label-width="formLabelWidth">
          <el-input
            v-model="form.avatar"
            auto-complete="on"
            @change="changea()"
            style="width: 50%"
          ></el-input>
        </el-form-item>

        <el-form-item
          label="管理员角色"
          :label-width="formLabelWidth"
          style="width: 50%"
        >
          <el-select v-model="value" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              style="width: 100%"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item
          label="密码"
          :label-width="formLabelWidth"
          style="width: 50%"
        >
          <el-input
            placeholder="请输入内容"
            v-model="form.password"
            type="password"
          >
          </el-input>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <!-- <el-input type="text" v-model="form.status" auto-complete="on" @change="changea()"></el-input> -->

          <label class="labela"
            ><input
              v-model="radio"
              type="radio"
              value="0"
              @change="getRadioVal"
            />启用</label
          >
          <label
            ><input
              v-model="radio"
              type="radio"
              value="1"
              @change="getRadioVal"
            />禁用</label
          >
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <!-- 设置触发更新的方法 -->
        <el-button type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      options: [
        {
          value: "1",
          label: "管理a",
        },
        {
          value: "2",
          label: "管理b",
        },
        {
          value: "3",
          label: "管理c",
        },
      ],
      value: "",
      radio: "1",
      //   表格的数据
      tableData: [],
      //   城市选择数据

      dialogFormVisible: false,
      dialogFormVisiblea: false,
      formLabelWidth: "100px",
      // 设置form用于进行添加的时候绑定值
      form: {
        name: "",
      },
      currentPage3: 1,
      currentIndex: "",

      RoleLists: [],
      roleID: [],
      input: "",
      roledalete: [],
      addAdmin: [],
      radioVal: "",
    };
  },

  created() {
    // this.roleLists();]
    this.userLists();
    this.add();
  },

  methods: {

    select(value) {
     this.item = this.options.find((item) => {
        return item.value === value
      })
      console.log("this.item",this.item);
    },

    getRadioVal(event) {
      this.radioVal = event.target.value;
      console.log(this.radioVal);
    },

    changea() {
      console.log("input值", this.form);
    },

    //管理员列表
    async userLists() {
      let a = await this.$api.home.adminLists();
      this.tableData = a.data.data;
      console.log("this.tableData", this.tableData);
    },

    //管理员添加
    // 增加数据的方式，单独的设置一些值，用于增加功能，这些值放在对象里面进行设置，然后将这个新增的对象塞到总数据里面
    add() {
      this.form = {
        name: "",
      };

      //   设置点击按钮之后进行显示对话框
      this.dialogFormVisiblea = true;
    },
    async update() {
      let b = (
        await this.$api.home.adminEdit({
          role: this.form.role,
          status: this.radioVal,
          username: this.form.username,
          nickname: this.form.nickname,
          password: this.form.password,
          avatar: this.form.avatar,
        })
      ).data;
      console.log("b", b);
      //   将我们添加的信息提交到总数据里面
      // this.tableData.push(this.form);
      this.dialogFormVisible = false;
      this.dialogFormVisiblea = false;
    },

    async updatea() {
      this.addAdmin = (await this.$api.home.aduserAdd({})).data;
      console.log("this.addAdmin", this.addAdmin);

      if (this.addAdmin.code == 401) {
        this.$notify.error({
          title: "错误",
          message: this.addAdmin.msg,
        });
      } else if (this.addAdmin.code == 200) {
        this.tableData.push(this.form);
      }

      this.dialogFormVisiblea = false;
    },

    handleEdit(index, row) {
      // 将数据的index传递过来用于实现数据的回显
      this.form = this.tableData[index];
      this.currentIndex = index;
      // 设置对话框的可见
      this.dialogFormVisible = true;
    },
    async handleDelete(index, row) {
      console.log("a", index, "b", row);
      console.log("row.id", row.id);

      this.roledalete = (
        await this.$api.home.roleDelete({
          id: row.id,
        })
      ).data;
      console.log("删除角色", this.roledalete);
      if (this.roledalete.code == 401) {
        this.$notify.error({
          title: "错误",
          message: this.roledalete.msg,
        });
      } else if (this.roledalete.code == 200) {
        this.tableData.splice(index, 1);
      }
    },
    cancel() {
      // 取消的时候直接设置对话框不可见即可
      this.dialogFormVisible = false;
      this.dialogFormVisiblea = false;
    },
  },
};
</script>

<style lang="less">
.tableMain {
  margin-top: 10px;
}

.page {
  float: left;
  margin-top: 10px;
}

.inputa {
  width: 70%;
}
.labela {
  margin-right: 20px;
}
</style>